﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace PersonaLibrary.PersonaliiReference
{
	partial class PersonalizationEntities
	{
		partial void OnContextCreated()
		{
			WritingEntity += PersonalizationEntities_WritingEntity;
		}

		void PersonalizationEntities_WritingEntity(object sender, System.Data.Services.Client.ReadingWritingEntityEventArgs e)
		{
			foreach (XElement node in e.Data.Elements())
			{
				if (node != null && node.Name.LocalName == "content")
				{
					foreach (XElement el in node.Elements())
					{
						if (el.Name.LocalName == "properties")
						{
							List<XElement> propertyToRemove = new List<XElement>();

							foreach (XElement prop in el.Elements())
							{
								if (
									e.Entity.GetType().GetProperty(prop.Name.LocalName).GetCustomAttributes(typeof(XmlIgnoreAttribute), true)
										.Length > 0)
								{
									propertyToRemove.Add(prop);
								}
							}

							propertyToRemove.ForEach(x => x.Remove());
						}
					}
				}
			}
		}

		
	}
}
